package com.example.booksystem.service.impl;

import com.example.booksystem.mapper.BookMapper;
import com.example.booksystem.model.Book;
import com.example.booksystem.service.BookService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookImpl implements BookService {
    private BookMapper bookMapper = new BookMapper();
    @Override
    public List<Book> selectBook(String text) throws SQLException {
        return bookMapper.selectBook(text);
    }
    @Override
    public List<Book> selectAllBook() throws SQLException {
        return bookMapper.selectAllBook();
    }

    @Override
    public Boolean deleteBook(Book book) throws SQLException {
        return bookMapper.deleteBook(book);
    }

    @Override
    public Boolean updateBook(Book book) throws SQLException {
        String id = book.getId();
        if (!"".equals(book.getTitle())){
            if (!bookMapper.updateBook("title",book.getTitle(),id)){
                return false;
            }
        }
        if (!"".equals(book.getPress())){
            if (!bookMapper.updateBook("press",book.getPress(),id)){
                return false;
            }
        }
        if (!"".equals(book.getAuthor())){
            if (!bookMapper.updateBook("author", book.getPress(), id)){
                return false;
            }
        }
        if (!"".equals(book.getPrice())){
            if (!bookMapper.updateBookPrice("price", book.getPrice(), id)){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean borrowBook(Book book,Integer userId) throws SQLException {
        return bookMapper.borrowBook(book,userId);
    }

    @Override
    public Boolean insertBook(Book book) throws SQLException {
        return bookMapper.insertBook(book);
    }

    @Override
    public List<Book> selectBorrowBook(Integer userId) throws SQLException {
        return bookMapper.selectBorrowBook(userId);
    }

    @Override
    public Boolean returnBook(Integer userId, String bookId) throws SQLException {
        if (bookMapper.deleteBorrowBook(userId,bookId) == 1){
            return true;
        }
        return false;
    }
}
